import { GameConfig } from "../configs/GameConfig";
import { Attribute } from "../etc/Attribute";
import { LogicDriver } from "../etc/LogicDriver";
import { add } from "../etc/Tween";
import { WorldUIPool } from "../etc/UIPool";
import { util } from "../etc/Util";
import { PowerGrowReward } from "../gameEtc/PowerGrowReward";
import { GuiModuleC } from "./GuiModule";
import { PlayerModuleS } from "./PlayerModule";

export class PickupModuleC extends ModuleC<PickupModuleS, null> {
    private pool: WorldUIPool<PowerGrowReward> = new WorldUIPool(PowerGrowReward)

    private generateIntervalMap: Map<number, number> = new Map()
    private generateRewardMap: Map<number, PowerGrowReward[]> = new Map()

    private generateRangeA: Vector
    private generateRangeB: Vector

    protected onStart(): void {
        this.generateRangeA = GameObject.findGameObjectById('2F8A0FE9').worldTransform.position
        this.generateRangeB = GameObject.findGameObjectById('15E6E765').worldTransform.position
        for (const cfg of GameConfig.PowerGrowReward.getAllElement()) {
            this.generateIntervalMap.set(cfg.ID, 0)
            this.generateRewardMap.set(cfg.ID, [])
        }
        LogicDriver.bindLogicLoop(this.onLogicUpdate.bind(this))
    }

    private onLogicUpdate() {
        // 生成
        this.generateChecker()
        // 拾取
        this.pickUpChecker()
    }

    protected onUpdate(dt: number): void {
        this.pool.eachVisibleItem(item => {
            item.onUpdate()
        })
    }

    private generateChecker(){
        for (const key of this.generateIntervalMap.keys()) {
            let curValue = this.generateIntervalMap.get(key)
            let cfg = GameConfig.PowerGrowReward.getElement(key)
            if (curValue < cfg.CreateInterval) {
                this.generateIntervalMap.set(key, curValue + 1)
                continue
            }
            // 重置倒计时
            this.generateIntervalMap.set(key, 0)
            // 生成
            this.createRewardByCfgID(key)
        }
    }

    private pickUpChecker(){
        let playerPosition = this.localPlayer.character.worldTransform.position
        for (const [key, array] of this.generateRewardMap) {
            for (const reward of array) {
                if(Vector.distance(reward.worldPosition, playerPosition) > 150) continue
                // 可拾取
                this.generateRewardMap.set(key, array.filter(item => item != reward))
                this.pool.giveBack(reward)
                // 报告服务器拾取
                this.server.net_pickup_power_grow_item(reward.cfg.ID)
                // 播放特效
                let position = Vector.add(playerPosition, new Vector(0,0,-80))
                EffectService.playAtPosition('142961', position)
                // ui动效
                let screenPosition = InputUtil.projectWorldPositionToWidgetPosition(playerPosition).screenPosition
                ModuleService.getModule(GuiModuleC).mainUI.showPowerTransition(reward.cfg.TransitionCount, screenPosition)
                return
            }
        }
    }

    private createRewardByCfgID(cfgID: number) {
        let cfg = GameConfig.PowerGrowReward.getElement(cfgID)
        // 已到达最大数量
        if (this.generateRewardMap.get(cfgID).length >= cfg.MaxCount) return
        // 生成
        let obj = this.pool.get()
        obj.setInfo(this.getGenerateRandomPosition(), cfg)
        this.generateRewardMap.get(cfgID).push(obj)
    }

    private getGenerateRandomPosition():Vector{
        return new Vector(
            util.lerp(this.generateRangeA.x, this.generateRangeB.x, Math.random()),
            util.lerp(this.generateRangeA.y, this.generateRangeB.y, Math.random()),
            util.lerp(this.generateRangeA.z, this.generateRangeB.z, 0.5),
        )
    }
}

export class PickupModuleS extends ModuleS<PickupModuleC, null> {
    net_pickup_power_grow_item(cfgID:number){
        let cfg = GameConfig.PowerGrowReward.getElement(cfgID)
        let playerMD = ModuleService.getModule(PlayerModuleS)
        playerMD.addPlayerAttr(this.currentPlayerId, Attribute.EnumAttributeType.power, cfg.GrowValue)
    }
}